library(tidyverse)
library(lubridate)
library(plotly)
library(hms)
d <- read.csv("E:/Phenocam_Analysis/CF_CF_9-dates.csv")%>%
  select(1,2,6,8)%>%
  mutate(datetime = ymd_hms(substr(img, 1, 19)),
         date = date(datetime),
         time = parse_hms(str_replace_all(substr(img, 12, 19), " ", ":")))%>%
  select('datetime', 'date', 'time', 'idx_label', 'RGB.2G_RBi', "RGB.pctG")
d_rem<- read.csv("C:/Users/ecotypes/Downloads/takeout.csv")
d1 = d%>%
  filter(!idx_label %in% d_rem$Index)
d2<-d1%>%
  group_by(date)%>%
  summarize(median = median(RGB.2G_RBi))
d3 <- d1 %>%
  right_join(d2, d1, by = 'date')%>%
  mutate(resid = RGB.2G_RBi - median)
d4 = d3%>%
  filter(resid < 10)%>%
  filter(resid > -10)
#  filter(time > hms(00,00,07))
plot_ly(d3, x = ~datetime, y = ~(RGB.2G_RBi-mean(RGB.2G_RBi, na.rm = TRUE)), hoverinfo = "text", hovertext = paste("Date: ", d3$datetime, "<br>2G_RBi: ", d3$RGB.2G_RBi, "<br>Index: ", d3$idx_label), type = "scatter", mode = "lines+markers")%>%
  layout(xaxis=list(title = "Date"), yaxis = list(title = "2G_RBi"))
z_score <- function(x) {z <- (x-mean(x, na.rm = TRUE))/sd(x, na.rm = TRUE)

return(z)}
x <- (1:20)

z_score(x)
##  [1] -1.60579308 -1.43676223 -1.26773138 -1.09870053 -0.92966968 -0.76063883
##  [7] -0.59160798 -0.42257713 -0.25354628 -0.08451543  0.08451543  0.25354628
## [13]  0.42257713  0.59160798  0.76063883  0.92966968  1.09870053  1.26773138
## [19]  1.43676223  1.60579308
d2 <- read.csv("C:/Users/ecotypes/Downloads/Total Green Length - Coldfoot.csv")%>%
  select(1:18)
rates2 <- d2%>%
  # mutate(X201 = X201/X153,
  #       X194 = X194/X153,
  #       X187 = X187/X153,
  #       X180 = X180/X153,
  #       X173 = X173/X153,
  #       X166 = X166/X153,
  #       X160 = X160/X153,
  #       X153 = 1)%>%
  pivot_longer(6:18, names_to = "Day", values_to = "Rate")%>%
  mutate(date = floor_date(as.Date(as.numeric(substr(Day, 2,4))-1, origin = "2022-01-01")),
         Loc = "CF")%>%
  filter(Plot == "CF9")%>%
  filter(Ind == "A")

joined <- full_join(d3, rates2, by = "date")%>%
  mutate(z_EG = z_score(RGB.2G_RBi),
         z_GCC = z_score(RGB.pctG),
         z_TGL = z_score(Rate))
plot_ly(d4, x = ~datetime, y = ~RGB.2G_RBi, hoverinfo = "text", hovertext = paste("Date: ", d4$datetime, "<br>2G_RBi: ", d4$RGB.2G_RBi, "<br>Index: ", d4$idx_label), type = "scatter", mode = "lines+markers")%>%
  layout(xaxis=list(title = "Date"), yaxis = list(title = "2G_RBi"))
ggplot(joined)+
  geom_point(aes(x = date, y = z_EG))+
  geom_point(aes(x = date, y = z_GCC), color = "Blue")+
  geom_point(aes(x = date, y = z_TGL), color = "Red", size = 3)+
  theme_bw()
## Warning: Removed 1 rows containing missing values (geom_point).
## Removed 1 rows containing missing values (geom_point).
## Warning: Removed 976 rows containing missing values (geom_point).

ggplot(d3, aes(x = time, y = resid))+
  geom_point(alpha = 0.1)+
  geom_smooth(method = "lm")+
  labs(y = "Distance from daily mean", x = "Time of Day")
## `geom_smooth()` using formula 'y ~ x'

d5 <- d3%>%
  mutate(datetime = floor_date(datetime, unit = "hour"),
         time = as.factor(hour(datetime)))%>%
  group_by(time)

ggplot(d5, aes(y = resid, group = time))+
  geom_boxplot()+
  labs(y = "Distance from daily mean", x = "Time of Day")